package org.example.judge_comment.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.judge_comment.common.RequestAnnouncementParams;
import org.example.judge_comment.service.AnnounceService;
import org.example.judge_common.annotation.AuthAnnotation;
import org.example.judge_common.entity.PageResult;
import org.example.judge_common.entity.RequestParams;
import org.example.judge_common.entity.ResponseResult;
import org.example.judge_pojo.Announcement;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/comment/announcement")
public class AnnouncementController {

    @Qualifier("AnnouncementServiceImpl")
    @Autowired
    private AnnounceService announceService;


    /**
     * 查询
     * @param params
     * @return
     */
    @PostMapping("/search")
    public ResponseResult list(RequestAnnouncementParams params){

        PageResult<Announcement> res = announceService.search(params);
        return ResponseResult.success().setData(res);
    }

    /**
     * 添加
     * @param announcement
     * @param request
     * @return
     */
    @AuthAnnotation({Auth.ADMINISTRATOR})
    @PostMapping("/save")
    public ResponseResult save(@RequestBody Announcement announcement, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
        announcement.setUserId(loginUser.getId());
        announceService.save(announcement);
        return ResponseResult.success();
    }

    /**
     * 更新
     * @param announcement
     * @param request
     * @return
     */
    @AuthAnnotation({Auth.ADMINISTRATOR})
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Announcement announcement, HttpServletRequest request){
        User loginUser = (User) request.getAttribute("loginUser");
        announcement.setUserId(loginUser.getId());
        announceService.updateById(announcement);
        return ResponseResult.success();
    }

    /**
     * 删除
     * @param announcementId
     * @return
     */
    @AuthAnnotation({Auth.ADMINISTRATOR})
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") String announcementId){
        announceService.removeById(announcementId);
        return ResponseResult.success();
    }
}


